﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Net.Sockets;
using System.Speech.Synthesis;
using System.Text;
using System.Windows.Forms;
using SongsDB;

namespace MMVoiceComm
{
    public class MMOptionEventClass
    {
        public static string selectedCommand = "";
        public void InitSheetOptions(SDBUITranspPanel sheet)
        {
            ConfigurationManager cm = new ConfigurationManager();
            //This might as well be seperate
            SDBApplication app = new SDBApplication();

            //Label for voices
            SDBUILabel voicesL = app.UI.get_NewLabel(sheet);
            voicesL.Caption = "Synthesized Voices:";
            voicesL.Common.SetRect(0, 0, 250, 20);
            voicesL.Common.ControlName = "voicesL";
            //ListBox that holds all the possibly voices
            SDBUIListBox voices = app.UI.get_NewListBox(sheet);
            voices.Common.ControlName = "voices";
            voices.Common.SetRect(0, 20, 450, 70);
            foreach (InstalledVoice iv in new SpeechSynthesizer().GetInstalledVoices())
            {
                voices.Items.Add(iv.VoiceInfo.Name);
                if (iv.VoiceInfo.Name.Equals(cm.GetConfigValue("voice")))
                {
                    voices.ItemIndex = voices.Items.Count - 1;
                }
            }
            
            //Computer repeat command
            SDBUICheckBox compSynthCommand = app.UI.get_NewCheckBox(sheet);
            compSynthCommand.Common.ControlName = "compSynthCommand";
            compSynthCommand.Caption = "Speak command back to you";
            compSynthCommand.Common.SetRect(0, 95, 210, 15);
            if (cm.GetConfigValue("synthRepeatCommand").ToLower().Equals("true")) compSynthCommand.Checked = true;

            //Enable/Disable start/stop listening
            SDBUICheckBox voiceToggleListen = app.UI.get_NewCheckBox(sheet);
            voiceToggleListen.Common.ControlName = "voiceToggleListen";
            voiceToggleListen.Caption = "Allow stop listening and start listening commands";
            voiceToggleListen.Common.SetRect(0, 115, 500, 15);
            if (cm.GetConfigValue("voiceToggleListen").ToLower().Equals("true")) voiceToggleListen.Checked = true;

            //Enable/Disable Auto Apply of Settings
            SDBUICheckBox autoAddSongs = app.UI.get_NewCheckBox(sheet);
            autoAddSongs.Common.ControlName = "autoAddSongs";
            autoAddSongs.Caption = "Auto Add All Songs Even if there are multiple songs matching";
            autoAddSongs.Common.SetRect(0, 135, 500, 15);
            if (cm.GetConfigValue("autoAddSongs").ToLower().Equals("true")) autoAddSongs.Checked = true;

            //Maximum jump distance
            SDBUISpinEdit jumpDistanceMax = app.UI.get_NewSpinEdit(sheet);
            jumpDistanceMax.Common.ControlName = "jumpDistanceMax";
            jumpDistanceMax.Common.SetRect(135, 155, 55, 30);
            jumpDistanceMax.MaxValue = 500;
            jumpDistanceMax.MinValue = 3;
            jumpDistanceMax.Value = 20;
            try
            {
                jumpDistanceMax.Value = Convert.ToInt32(cm.GetConfigValue("jumpDistanceMax"));
            }catch(Exception ex) {}
            //Companion Label
            SDBUILabel jumpDistanceMaxLabel = app.UI.get_NewLabel(sheet);
            jumpDistanceMaxLabel.Common.SetRect(0, 155, 50, 25);
            jumpDistanceMaxLabel.Caption = "Max Jump Distance:";

            //Maximum jump distance
            SDBUISpinEdit jumpDistanceMin = app.UI.get_NewSpinEdit(sheet);
            jumpDistanceMin.Common.ControlName = "jumpDistanceMin";
            jumpDistanceMin.Common.SetRect(135, 185, 55, 30);
            jumpDistanceMin.MaxValue = 499;
            jumpDistanceMin.MinValue = 1;
            jumpDistanceMin.Value = 20;
            try
            {
                jumpDistanceMin.Value = Convert.ToInt32(cm.GetConfigValue("jumpDistanceMin"));
            }
            catch (Exception ex) { }
            //Companion Label
            SDBUILabel jumpDistanceMinLabel = app.UI.get_NewLabel(sheet);
            jumpDistanceMinLabel.Common.SetRect(0, 185, 50, 25);
            jumpDistanceMinLabel.Caption = "Min Jump Distance:";


            //Enable/Disable logging
            SDBUICheckBox log = app.UI.get_NewCheckBox(sheet);
            log.Common.ControlName = "log";
            log.Caption = "Enable logging?";
            log.Common.SetRect(0, 220, 500, 15);
            if (Logger.isEnabled()) log.Checked = true;

            //Label for help
            SDBUILabel help = app.UI.get_NewLabel(sheet);
            help.Multiline = true;
            help.Caption = "Changes will not take place until MMVoiceComm is reloaded.";
            help.Common.SetRect(0, 390, 450, 40);
        }

      



        public void SaveSheetOptions(SDBUITranspPanel sheet)
        {
            //This might as well be seperate
            SDBApplication app = new SDBApplication();
            ConfigurationManager cm = new ConfigurationManager();

            //Synthesized Voice
            SDBUIListBox voices = (SDBUIListBox)sheet.Common.get_ChildControl("voices");
            cm.SetConfigValue("voice", voices.Items.get_Item(voices.ItemIndex));

            //Synthesize voice repeat command
            SDBUICheckBox compSynthCommand = (SDBUICheckBox)sheet.Common.get_ChildControl("compSynthCommand");
            cm.SetConfigValue("synthRepeatCommand", compSynthCommand.Checked.ToString());

            //enable start / stop listen
            SDBUICheckBox voiceToggleListen = (SDBUICheckBox)sheet.Common.get_ChildControl("voiceToggleListen");
            cm.SetConfigValue("voiceToggleListen", voiceToggleListen.Checked.ToString());

            //enable restart of MMVoiceComm on okay hit
            SDBUICheckBox autoAddSongs = (SDBUICheckBox)sheet.Common.get_ChildControl("autoAddSongs");
            cm.SetConfigValue("autoAddSongs", autoAddSongs.Checked.ToString());

            //jump distance
            SDBUISpinEdit jumpDistanceMin = (SDBUISpinEdit)sheet.Common.get_ChildControl("jumpDistanceMin");
            int jumpDistanceMinVal = jumpDistanceMin.Value;
            SDBUISpinEdit jumpDistanceMax = (SDBUISpinEdit)sheet.Common.get_ChildControl("jumpDistanceMax");
            int jumpDistanceMaxVal = jumpDistanceMax.Value;
            if (jumpDistanceMinVal < jumpDistanceMaxVal)
            {
                cm.SetConfigValue("jumpDistanceMin", jumpDistanceMinVal.ToString());
                cm.SetConfigValue("jumpDistanceMax", jumpDistanceMaxVal.ToString());
            }

            //Should it be logging?
            SDBUICheckBox log = (SDBUICheckBox)sheet.Common.get_ChildControl("log");
            Logger.setEnabled(log.Checked);

            //Save the config
            cm.WriteConfig();

            if (cm.GetConfigValue("autoApplySettings").ToLower().Equals("true"))
            {
                UdpClient udp = new UdpClient();
                udp.Send(new byte[1]{ Convert.ToByte(MMStart.networkMessages.reinitAll)},1, "127.0.01", 6589);
                udp.Close();
            }
        }

        public void InitSheetCommands(SDBUITranspPanel sheet)
        {
            //This might as well be seperate
            SDBApplication app = new SDBApplication();
            ConfigurationManager cm = new ConfigurationManager();

            

            //Label for title
            SDBUILabel title = app.UI.get_NewLabel(sheet);
            title.Caption = "Select a command then hit the 'configure' button to configure ways to say it";

            //Label for help
            SDBUILabel help = app.UI.get_NewLabel(sheet);
            help.Multiline = true;
            help.Caption = "Changes will not take place until MMVoiceComm is reloaded.";
            help.Common.SetRect(0,390,450,40);

            //Contains all the commands
            SDBUIListBox commandList = app.UI.get_NewListBox(sheet);
            commandList.Common.ControlName = "commandList";
            commandList.Common.SetRect(0, 20, 450, 325);
            //For some reason it seems to work when i do this before the real one...
            commandList.OnChange += commandList_OnChange;
            commandList.OnChange -= commandList_OnChange;
            foreach(KeyValuePair<string,List<string>> kvp in cm.Commands)
            {
                commandList.Items.Add(kvp.Key);
            }
            commandList.OnChange += commandList_OnChange;


            SDBUIButton configureButton = app.UI.get_NewButton(sheet);
            
            
            //just to be safe
            configureButton.Common.ControlName = "configureButton";
            configureButton.OnClick += configureButton_OnClick;
            configureButton.Common.SetRect(20, 350, 50, 25);
            configureButton.OnClick -= configureButton_OnClick;
            configureButton.Caption = "Configure";

            //Okay it better work
            configureButton.OnClick += configureButton_OnClick;
        }

        public static void configureButton_OnClick()
        {

            CommandConfigForm ccf = new CommandConfigForm(selectedCommand);
            ccf.Show();
        }

        public void commandList_OnChange(object Control)
        {


            SDBUIListBox cmdList = (SDBUIListBox) Control;
            

            if(selectedCommand.Equals(cmdList.Items.get_Item(cmdList.ItemIndex)))
            {
                selectedCommand = cmdList.Items.get_Item(cmdList.ItemIndex);
            }else
            {
                selectedCommand = cmdList.Items.get_Item(cmdList.ItemIndex);
                
            }

    }

        
        public void SaveSheetCommands(SDBUITranspPanel sheet)
        {

            //This might as well be seperate
            SDBApplication app = new SDBApplication();
            ConfigurationManager cm = new ConfigurationManager();

            if (cm.GetConfigValue("autoApplySettings").ToLower().Equals("true"))
            {
                UdpClient udp = new UdpClient();
                udp.Send(new byte[1] { Convert.ToByte(MMStart.networkMessages.reinitAll) }, 1, "127.0.01", 6589);
                udp.Close();
            }
        }
    }
}
